#include "prober.h"

Prober::Prober(QObject *parent) : QObject(parent)
  ,serverPort(8888)
{
    tcpserver = new QTcpServer(this);
    tcpserver->listen(QHostAddress::Any, serverPort);
    connect (tcpserver, SIGNAL(newConnection()), this, SLOT(updateserver()));
}

Prober::~Prober()
{
}

void Prober::updateserver()
{
    emit sendLinkStatus(QStringLiteral("已连接"));

    QTcpSocket *tcpclient = tcpserver->nextPendingConnection();
    if(!tcpclient)
    {
        return;
    }

    QString clientip = tcpclient->peerAddress().toString();
    if(clientip.isNull() || clientip.isEmpty())
    {
        tcpclient->disconnectFromHost();
        return;
    }
    linkList.append(tcpclient);

    connect (tcpclient, SIGNAL(disconnected()), this, SLOT(updateSendStatus()));
    connect (tcpclient, SIGNAL(readyRead()), this, SLOT(readServerMessage()));

    updateListInfo();
}
/*-------------------------------------------
*
* 停止监听
*
---------------------------------------------*/
void Prober::closeListen()
{
    foreach(QTcpSocket* socket ,linkList)
    {
        if(!socket)
        {
            continue;
        }
        socket->disconnect();
        socket->close();
        socket->deleteLater();
    }
    tcpserver->close();

    emit sendLinkStatus(QStringLiteral("停止监听"));
    linkList.clear();
    updateListInfo();
}
/*-------------------------------------------
*
* 打开监听
*
---------------------------------------------*/
bool Prober::openListen()
{
    if(!tcpserver)
    {
        return false;
    }

    tcpserver->listen(QHostAddress::Any, serverPort);

    emit sendLinkStatus(QStringLiteral("开始监听"));
    return true;
}
/*-------------------------------------------
*
* 处理无连接
*
---------------------------------------------*/
void Prober::updateSendStatus()
{
    emit sendLinkStatus(QStringLiteral("无连接"));
    QTcpSocket *tcpSocket = qobject_cast<QTcpSocket *>(sender());
    linkList.removeOne(tcpSocket);
    tcpSocket->disconnect();
    tcpSocket->close();
    tcpSocket->deleteLater();

    updateListInfo();
}
/*-------------------------------------------
*
* 处理接收到的数据
*
---------------------------------------------*/
void Prober::readServerMessage()
{
    QTcpSocket *tcpSocket = qobject_cast<QTcpSocket *>(sender());
    if(!tcpSocket)
    {
        return;
    }

    QByteArray byte_data=tcpSocket->readAll();

    QString message = QString(byte_data);

    if(message.contains(QString("e8:4e:06:38:67:14"), Qt::CaseInsensitive))
    {
        qDebug()<<QString("e8:4e:06:38:67:14");
    }

    message.replace('"',"");
    message.replace(" }","");
    message.replace(" ]","");

    ProberListStruct proberList;
    proberList.device_sn = "";
    QStringList messageList = message.split("{");
    foreach (QString macmsg, messageList)
    {
        QStringList maclist = macmsg.split(",");
        /*-------------------------------------------
        * 找出SN
        ---------------------------------------------*/
        if( macmsg.contains("id", Qt::CaseInsensitive))
        {
            if(maclist.count() < 3 && maclist.count() > 1)
            {
                QStringList msglist = macmsg.split(",");
                if(msglist.count()> 0)
                {
                    QStringList snlist = msglist[0].split(": ");
                    if(snlist.count()> 1)
                    {
                        proberList.device_sn = snlist[1];
                    }
                }
            }
        }
        /*-------------------------------------------
        * 找出MAC
        ---------------------------------------------*/
        if(!proberList.device_sn.isEmpty())
        {
            ProberStruct m_probers;
            if(macmsg.contains("mac", Qt::CaseInsensitive))
            {
                QStringList prolist = macmsg.split(",");
                if(prolist.count()> 2)
                {
                    /*-------------------------------------------
                    * MAC
                    ---------------------------------------------*/
                    if(prolist[0].contains("mac", Qt::CaseInsensitive))
                    {
                        QStringList maclist = prolist[0].split(": ");
                        if(maclist.count()> 1)
                        {
                            m_probers.mac = maclist[1];
                        }
                    }
                    /*-------------------------------------------
                    * rssi
                    ---------------------------------------------*/
                    if(prolist[1].contains("rssi", Qt::CaseInsensitive))
                    {
                        QStringList rssilist = prolist[1].split(": ");
                        if(rssilist.count()> 1)
                        {
                            m_probers.rssi = rssilist[1];
                        }
                    }
                    /*-------------------------------------------
                    * ch
                    ---------------------------------------------*/
                    if(prolist[2].contains("ch", Qt::CaseInsensitive))
                    {
                        QStringList chlist = prolist[2].split(": ");
                        if(chlist.count()> 1)
                        {
                            m_probers.chenal = chlist[1];
                        }
                    }
                    proberList.probers.append(m_probers);
                }
            }
        }
    }

    if(!proberList.device_sn.isEmpty())
    {
        emit sendProberList(proberList);
    }
}
/*-------------------------------------------
*
* 更新连接信息列表
*
---------------------------------------------*/
void Prober::updateListInfo()
{
    foreach(QTcpSocket *socket,linkList)
    {
        if(!socket)
        {
            continue;
        }
        //QString name = socket->peerName();
        //QString ip = socket->peerAddress().toString();
    }
}
/*-------------------------------------------
*
* 开始
*
---------------------------------------------*/
void Prober::startListen()
{
    closeListen();
    openListen();
}
